<template>
  <div class="app-container" v-enter-helper="getList">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="danger" @click="handleDeleteAll">批量删除</el-button>
          <el-button type="danger" @click="handleFreezeAll">批量冻结</el-button>
          <el-button type="primary" @click="handleExport">导出</el-button>
          <el-button type="primary" @click="handleExportAsync">
            异步导出
          </el-button>
          <el-form
            :inline="true"
            :model="listQuery"
            ref="dataList2"
            class="demo-form-inline"
          >
            <el-form-item label="">
              <el-select
                filterable
                v-model="listQuery.gift_id"
                placeholder="选择礼物"
                clearable
              >
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="">
              <el-input
                type="text"
                v-model="listQuery.nickname"
                placeholder="昵称"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input
                type="text"
                v-model="listQuery.user_code"
                placeholder="用户ID"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchSubmit">查询</el-button>
              <el-button type="primary" @click="resetSubmit">重置</el-button>
            </el-form-item>
          </el-form>
        </el-row>
      </div>
      <div class="item">
        <el-table
          v-loading="listLoading"
          ref="dataList"
          :data="menuList"
          @selection-change="handleSelectionChange"
          style="width:100%;margin-bottom: 20px;"
          row-key="id"
          border
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column type="selection"></el-table-column>
          <el-table-column prop="id" label="序号"> </el-table-column>
          <el-table-column prop="user_code" label="用户ID"> </el-table-column>
          <el-table-column prop="nickname" label="昵称"> </el-table-column>
          <el-table-column prop="name" label="名称"> </el-table-column>
          <!-- <el-table-column prop="gift_id" label="名称" >
            <template slot-scope="scope">
              <div v-for="item in typeList" :key="item.value">
                <p v-if="item.value === scope.row.gift_id">{{item.label}}</p>
              </div>
            </template>
          </el-table-column> -->
          <el-table-column prop="price" label="价格">
            <template slot-scope="scope"> {{ scope.row.price }}金币 </template>
          </el-table-column>
          <el-table-column prop="number" label="数量"> </el-table-column>
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                plain
                >删除</el-button
              >
              <el-button
                type="primary"
                v-if="scope.row.state === 1"
                size="mini"
                icon="el-icon-delete"
                @click="handleFreeze(scope.row)"
                plain
                >冻结礼物</el-button
              >
              <el-button
                type="info"
                v-if="scope.row.state === 2"
                size="mini"
                icon="el-icon-delete"
                @click="handleCancelFreeze(scope.row)"
                plain
                >解冻礼物</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.perPage"
          @pagination="getList"
        />
      </div>
    </el-card>
    <!-- 删除弹框 -->
    <el-dialog
      title="确定删除吗?"
      :visible.sync="deleteDialogVisible"
      width="435px"
      @close="handleDialogClose('editRulesForm')"
    >
      <span style=""
        ><em style="font-style:normal;color:red">删除后不可恢复</em
        >，你还要继续吗？</span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 冻结弹框 -->
    <el-dialog title="信息" :visible.sync="freezeDialogVisible">
      <span style="">确认冻结吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="freezeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmFreeze">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 解冻弹框 -->
    <el-dialog title="信息" :visible.sync="cancelFreezeDialogVisible">
      <span style="">确认解冻吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelFreezeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmCancelFreeze">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listData,
  listTypeData,
  deleteData,
  postFreeze,
  postFreezeAll,
  getExport,
  getExportAsync
} from "@/api/member/backpack";
export default {
  data() {
    return {
      confirmBtn: false,

      row_state: undefined,
      row_id: undefined,
      cancelFreezeDialogVisible: false,
      freezeDialogVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [],
      total: 10,
      listQuery: {
        page: 1,
        perPage: 10,
        gift_id: undefined,
        nickname: undefined,
        user_code: undefined
      },
      // 礼物列表
      typeList: [],
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: null
    };
  },
  created() {
    this.getList();
    this.getTypeList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      let urlData = "?";
      if (this.listQuery.page) {
        urlData = urlData + `page=${this.listQuery.page}&`;
      }
      if (this.listQuery.perPage) {
        urlData = urlData + `perPage=${this.listQuery.perPage}`;
      }
      if (this.listQuery.user_code) {
        urlData = urlData + `&user_code=${this.listQuery.user_code}`;
      }
      if (this.listQuery.nickname) {
        urlData = urlData + `&nickname=${this.listQuery.nickname}`;
      }
      if (this.listQuery.gift_id) {
        urlData = urlData + `&gift_id=${this.listQuery.gift_id}`;
      }

      listData(urlData).then(response => {
        this.menuList = response.data.list;
        this.total = response.data.meta.total;
        this.listLoading = false;
        this.confirmBtn = false;
      });
    },
    getTypeList() {
      let obj = {
        page: 1,
        perPage: 10000
      };
      listTypeData(obj).then(response => {
        response.data.list.forEach(item => {
          this.typeList.push({ value: item.id, label: item.name });
        });
      });
    },
    // 查询
    searchSubmit() {
      this.listQuery.page = 1;
      this.getList();
    },
    // 重置
    resetSubmit() {
      this.listQuery.page = 1;
      this.listQuery.perPage = 10;
      this.listQuery.gift_id = undefined;
      this.listQuery.nickname = undefined;
      this.listQuery.user_code = undefined;

      // this.getList()
    },

    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 点击单个删除
    handleDelete(row) {
      this.dataIds = { ids: row.id.toString() };
      this.deleteDialogVisible = true;
    },
    // 点击批量删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: "请先勾选要删除的内容",
          type: "warning"
        });
        return;
      } else if (this.multipleSelection.length !== 0) {
        let ids = "";
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`;
        }
        ids = ids.substring(0, ids.length - 1);
        this.dataIds = { ids };
      }
      this.deleteDialogVisible = true;
    },
    // 确定删除
    confirmDelete() {
      if (this.confirmBtn) return;
      this.confirmBtn = true;
      deleteData(this.dataIds).then(response => {
        if (response.code === 200) {
          this.getList();
          this.deleteDialogVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        } else {
          this.confirmBtn = false;
        }
      });
    },
    // 冻结礼物
    handleFreeze(row) {
      this.row_id = row.id;
      this.row_state = 2;
      this.freezeDialogVisible = true;
    },
    // 确认冻结礼物
    confirmFreeze() {
      postFreeze(this.row_id, { state: this.row_state }).then(response => {
        if (response.code === 200) {
          this.getList();
          this.freezeDialogVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        } else {
          this.$message({
            message: response.message,
            type: "error"
          });
        }
      });
    },
    // 冻结礼物
    handleCancelFreeze(row) {
      this.row_id = row.id;
      this.row_state = 1;
      this.cancelFreezeDialogVisible = true;
    },
    // 确认冻结礼物
    confirmCancelFreeze() {
      postFreeze(this.row_id, { state: this.row_state }).then(response => {
        if (response.code === 200) {
          this.getList();
          this.cancelFreezeDialogVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        } else {
          this.$message({
            message: response.message,
            type: "error"
          });
        }
      });
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate(); // 清除整个表单的校验
      }
    },

    /**批量冻结 */
    handleFreezeAll() {
      console.log(this.multipleSelection);
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: "请先勾选内容",
          type: "warning"
        });
        return;
      }
      if (this.multipleSelection.length > 0) {
        let arr = [];
        this.multipleSelection.forEach(item => {
          if (item.state === 1) {
            arr.push(item.id);
          }
        });
        this.dataIds = arr.join(",");
        postFreezeAll(this.dataIds).then(res => {
          if (res.code === 200) {
            this.getList();
            this.cancelFreezeDialogVisible = false;
            this.$notify({
              type: "success",
              message: "操作成功",
              position: "bottom-right",
              showClose: false,
              duration: 3000
            });
          } else {
            this.$message({
              message: res.message,
              type: "error"
            });
          }
        });
      }
    },
    /**导出 */
    handleExport() {
      let obj = {
        user_code: undefined,
        gift_id: undefined,
        nickname: undefined
      };
      for (let key in obj) {
        obj[key] = this.listQuery[key];
      }

      getExport(obj).then(res => {
        if (res.code === 200) {
          window.location.href = res.data.uri;
        }
      });
    },
    handleExportAsync() {
      let obj = {
        user_code: undefined,
        gift_id: undefined,
        nickname: undefined
      };
      for (let key in obj) {
        obj[key] = this.listQuery[key];
      }

      getExportAsync(obj).then(res => {
        if (res.code === 200) {
          this.$message.success(
            "正在导出，请稍等！可移步到 订单管理-导出日志 查看下载链接"
          );
        }
      });
    }
  }
};
</script>

<style scoped>
[v-cloak] {
  display: none;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}

.demo-form-inline {
  float: right;
}

/deep/.el-card__header {
  padding-bottom: 0;
}
</style>
